﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AbingtonBooks.Pages.Login
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Customer"] != null) {
                Response.Redirect("~/Default.aspx");
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            using (dbAbingtonBooks context = new dbAbingtonBooks()) {
                Customer thisCustomer = context.Customers.Where(x => x.Username == txtUser.Text).FirstOrDefault();
                if (thisCustomer != null)
                {
                    string enteredPassword = txtPassword.Text;
                    if (enteredPassword == thisCustomer.Password)
                    {
                        Session["Customer"] = thisCustomer;
                        if (Session["ToGo"] != null)
                        {
                            Response.Redirect(Session["ToGo"].ToString());
                        }
                        else
                        {
                            Response.Redirect("~/Default.aspx");
                        }
                    }
                    else
                    {
                        lblError.Text = "Incorrect Login information";
                    }
                }
                else {
                    lblError.Text = "Incorrect Login information";
                }
            }
        }
    }
}